﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Drawing.Drawing2D;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace HML.Demo
{
    public partial class SingleAccordionStyle4EffectForm : ItemFormTemplate
    {
        public SingleAccordionStyle4EffectForm()
        {
            InitializeComponent();
            this.Load += new System.EventHandler(this.Form_Load);
            this.Shown += new System.EventHandler(this.Form_Shown);
        }

        #region 实现

        public override ItemFormTemplateViewObject AsynchRequestTask(ItemFormTemplateViewObject view)
        {
            System.Threading.Thread.Sleep(300);
            view.ViewData = DemoHelper.GetDescription(AppDomain.CurrentDomain.BaseDirectory + "例子\\控件效果\\手风琴\\SingleAccordionStyle4EffectForm.rtf");
            return view;
        }

        public override void DataBind(ItemFormTemplateViewObject viewObject)
        {
            this.introduceBrowser.Rtf = viewObject.ViewData.ToString();
        }

        public override void ShowView()
        {
            base.ShowView();
        }

        public override void HideView()
        {
            base.HideView();
        }

        public override void CloseView()
        {
            base.CloseView();
        }

        #endregion

        #region 属性面板

        /// <summary>
        /// 信息通知
        /// </summary>
        private Dictionary<string, string> infoDic = new Dictionary<string, string>() {
            {"Staff" ,"3"}
        };

        private void Form_Load(object sender, EventArgs e)
        {
            this.controlContainer.SizeChanged += this.ControlContainer_SizeChanged;


            List<SingleAccordionNode> nodeList = new List<SingleAccordionNode>();
            {
                SingleAccordionCategoryNode singleAccordionCategoryNode1 = new SingleAccordionCategoryNode() { Text = "Admin" };
                nodeList.Add(singleAccordionCategoryNode1);
                singleAccordionCategoryNode1.Childrens.Add(new HML.SingleAccordionContentNode() { Text = "Payments", ImageVisible = SingleAccordionNodeImageVisibles.True, ImageLRMargin = new LRMargin(8, 8), Image = global::HML.Demo.Properties.Resources.控件效果_SingleAccordion_Style4_Payments, TextLRMargin = new LRMargin(0, 0) });
                singleAccordionCategoryNode1.Childrens.Add(new HML.SingleAccordionContentNode() { Text = "Noticeboard", ImageVisible = SingleAccordionNodeImageVisibles.True, ImageLRMargin = new LRMargin(8, 8), Image = global::HML.Demo.Properties.Resources.控件效果_SingleAccordion_Style4_Noticeboard, TextLRMargin = new LRMargin(0, 0) });
                singleAccordionCategoryNode1.Childrens.Add(new HML.SingleAccordionContentNode() { Text = "Taskboard", ImageVisible = SingleAccordionNodeImageVisibles.True, ImageLRMargin = new LRMargin(8, 8), Image = global::HML.Demo.Properties.Resources.控件效果_SingleAccordion_Style4_Taskboard, TextLRMargin = new LRMargin(0, 0) });
                singleAccordionCategoryNode1.Childrens.Add(new HML.SingleAccordionContentNode() { Text = "Hostel", ImageVisible = SingleAccordionNodeImageVisibles.True, ImageLRMargin = new LRMargin(8, 8), Image = global::HML.Demo.Properties.Resources.控件效果_SingleAccordion_Style4_Hostel, TextLRMargin = new LRMargin(0, 0) });
                singleAccordionCategoryNode1.Childrens.Add(new HML.SingleAccordionContentNode() { Text = "Transport", ImageVisible = SingleAccordionNodeImageVisibles.True, ImageLRMargin = new LRMargin(8, 8), Image = global::HML.Demo.Properties.Resources.控件效果_SingleAccordion_Style4_Transport, TextLRMargin = new LRMargin(0, 0) });
                singleAccordionCategoryNode1.Childrens.Add(new HML.SingleAccordionContentNode() { Text = "Attendance", ImageVisible = SingleAccordionNodeImageVisibles.True, ImageLRMargin = new LRMargin(8, 8), Image = global::HML.Demo.Properties.Resources.控件效果_SingleAccordion_Style4_Attendance, TextLRMargin = new LRMargin(0, 0) });
                singleAccordionCategoryNode1.Childrens.Add(new HML.SingleAccordionContentNode() { Text = "Leave", ImageVisible = SingleAccordionNodeImageVisibles.True, ImageLRMargin = new LRMargin(8, 8), Image = global::HML.Demo.Properties.Resources.控件效果_SingleAccordion_Style4_Leave, TextLRMargin = new LRMargin(0, 0) });
                singleAccordionCategoryNode1.Childrens.Add(new HML.SingleAccordionContentNode() { Text = "Settings", ImageVisible = SingleAccordionNodeImageVisibles.True, ImageLRMargin = new LRMargin(8, 8), Image = global::HML.Demo.Properties.Resources.控件效果_SingleAccordion_Style4_Settings, TextLRMargin = new LRMargin(0, 0) });

                SingleAccordionCategoryNode singleAccordionCategoryNode2 = new SingleAccordionCategoryNode() { Text = "University" };
                nodeList.Add(singleAccordionCategoryNode2);
                singleAccordionCategoryNode2.Childrens.Add(new HML.SingleAccordionContentNode() { Text = "Dashboard", ImageVisible = SingleAccordionNodeImageVisibles.True, ImageLRMargin = new LRMargin(8, 8), Image = global::HML.Demo.Properties.Resources.控件效果_SingleAccordion_Style4_Dashboard, TextLRMargin = new LRMargin(0, 0) });
                singleAccordionCategoryNode2.Childrens.Add(new HML.SingleAccordionContentNode() { Text = "Professors", ImageVisible = SingleAccordionNodeImageVisibles.True, ImageLRMargin = new LRMargin(8, 8), Image = global::HML.Demo.Properties.Resources.控件效果_SingleAccordion_Style4_Professors, TextLRMargin = new LRMargin(0, 0) });
                singleAccordionCategoryNode2.Childrens.Add(new HML.SingleAccordionContentNode() { Text = "Staff", ImageVisible = SingleAccordionNodeImageVisibles.True, ImageLRMargin = new LRMargin(8, 8), Image = global::HML.Demo.Properties.Resources.控件效果_SingleAccordion_Style4_Staff, TextLRMargin = new LRMargin(0, 0) });
                singleAccordionCategoryNode2.Childrens.Add(new HML.SingleAccordionContentNode() { Text = "Students", ImageVisible = SingleAccordionNodeImageVisibles.True, ImageLRMargin = new LRMargin(8, 8), Image = global::HML.Demo.Properties.Resources.控件效果_SingleAccordion_Style4_Students, TextLRMargin = new LRMargin(0, 0) });
                singleAccordionCategoryNode2.Childrens.Add(new HML.SingleAccordionContentNode() { Text = "Departments", ImageVisible = SingleAccordionNodeImageVisibles.True, ImageLRMargin = new LRMargin(8, 8), Image = global::HML.Demo.Properties.Resources.控件效果_SingleAccordion_Style4_Departments, TextLRMargin = new LRMargin(0, 0) });
                singleAccordionCategoryNode2.Childrens.Add(new HML.SingleAccordionContentNode() { Text = "Courses", ImageVisible = SingleAccordionNodeImageVisibles.True, ImageLRMargin = new LRMargin(8, 8), Image = global::HML.Demo.Properties.Resources.控件效果_SingleAccordion_Style4_Courses, TextLRMargin = new LRMargin(0, 0) });
                singleAccordionCategoryNode2.Childrens.Add(new HML.SingleAccordionContentNode() { Text = "Library", ImageVisible = SingleAccordionNodeImageVisibles.True, ImageLRMargin = new LRMargin(8, 8), Image = global::HML.Demo.Properties.Resources.控件效果_SingleAccordion_Style4_Library, TextLRMargin = new LRMargin(0, 0) });
                singleAccordionCategoryNode2.Childrens.Add(new HML.SingleAccordionContentNode() { Text = "Holiday", ImageVisible = SingleAccordionNodeImageVisibles.True, ImageLRMargin = new LRMargin(8, 8), Image = global::HML.Demo.Properties.Resources.控件效果_SingleAccordion_Style4_Holiday, TextLRMargin = new LRMargin(0, 0) });

                SingleAccordionCategoryNode singleAccordionCategoryNode3 = new SingleAccordionCategoryNode() { Text = "EXTRA" };
                nodeList.Add(singleAccordionCategoryNode3);
                singleAccordionCategoryNode3.Childrens.Add(new HML.SingleAccordionContentNode() { Text = "Calender", ImageVisible = SingleAccordionNodeImageVisibles.True, ImageLRMargin = new LRMargin(8, 8), Image = global::HML.Demo.Properties.Resources.控件效果_SingleAccordion_Style4_Calender, TextLRMargin = new LRMargin(0, 0) });
                singleAccordionCategoryNode3.Childrens.Add(new HML.SingleAccordionContentNode() { Text = "Chat App", ImageVisible = SingleAccordionNodeImageVisibles.True, ImageLRMargin = new LRMargin(8, 8), Image = global::HML.Demo.Properties.Resources.控件效果_SingleAccordion_Style4_ChatApp, TextLRMargin = new LRMargin(0, 0) });
                singleAccordionCategoryNode3.Childrens.Add(new HML.SingleAccordionContentNode() { Text = "Contact", ImageVisible = SingleAccordionNodeImageVisibles.True, ImageLRMargin = new LRMargin(8, 8), Image = global::HML.Demo.Properties.Resources.控件效果_SingleAccordion_Style4_Contact, TextLRMargin = new LRMargin(0, 0) });
                singleAccordionCategoryNode3.Childrens.Add(new HML.SingleAccordionContentNode() { Text = "FileManager", ImageVisible = SingleAccordionNodeImageVisibles.True, ImageLRMargin = new LRMargin(8, 8), Image = global::HML.Demo.Properties.Resources.控件效果_SingleAccordion_Style4_FileManager, TextLRMargin = new LRMargin(0, 0) });
                singleAccordionCategoryNode3.Childrens.Add(new HML.SingleAccordionContentNode() { Text = "Gallery", ImageVisible = SingleAccordionNodeImageVisibles.True, ImageLRMargin = new LRMargin(8, 8), Image = global::HML.Demo.Properties.Resources.控件效果_SingleAccordion_Style4_Gallery, TextLRMargin = new LRMargin(0, 0) });

            }
            this.demo_control.Size = new Size((int)Math.Ceiling(225 * this.demo_control.ScaleDpi), (int)Math.Ceiling(580 * this.demo_control.ScaleDpi));
            this.demo_control.BackColor = Color.Transparent;
            this.demo_control.Font = new Font("Bahnschrift SemiBold", 9.5F);
            this.demo_control.CategoryNodeStyle.ECAnimationAllTimer = 350;
            this.demo_control.CategoryNodeStyle.TextLRMargin = new LRMargin(8, 0);
            this.demo_control.StyleAppearance.CategoryNode.Enter.BackColor = Color.Transparent;
            this.demo_control.StyleAppearance.CategoryNode.Normal.BackColor = Color.Transparent;
            this.demo_control.StyleAppearance.CategoryNode.Normal.TextColor = Color.FromArgb(255, 255, 255);
            this.demo_control.StyleAppearance.CategoryNode.Enter.TextColor = Color.FromArgb(255, 255, 255);
            this.demo_control.ContentNodeStyle.TextLRMargin = new LRMargin(30, 0);
            this.demo_control.StyleAppearance.ContentNode.Enter.BackColor = Color.Transparent;
            this.demo_control.StyleAppearance.ContentNode.Normal.BackColor = Color.Transparent;
            this.demo_control.StyleAppearance.ContentNode.Selected.BackColor = Color.Transparent;
            this.demo_control.ContentNodeStyle.LRAnimationDistance = 0;
            this.demo_control.ContentNodeStyle.MouseAnimationAllTimer = 150;
            this.demo_control.StyleAppearance.ContentNode.Normal.TextColor = Color.White;
            this.demo_control.StyleAppearance.ContentNode.Selected.TextColor = Color.White;
            this.demo_control.ScrollBar.AutoHide = true;
            this.demo_control.Nodes.Clear();
            this.demo_control.Nodes.AddRange(nodeList.ToArray());
            this.demo_control.BackgroundDrawBefore += this.Demo_control_BackgroundDrawBefore;
            this.demo_control.NodeDrawBefore += this.Demo_control_NodeDrawBefore;
            this.demo_control.NodeDrawAfter += this.Demo_control_NodeDrawAfter;
            this.demo_control.NodeClick += this.Demo_control_NodeClick;

        }

        private void Form_Shown(object sender, EventArgs e)
        {
            this.splitContainer.SplitterDistance = Math.Max(this.splitContainer.Width / 4, this.splitContainer.Width - MainForm.GlobalSplitWidth);
            this.splitContainer.SplitterMoved += this.SplitContainer_SplitterMoved;

            MainForm.LoadAssistTabControl(this.assistTabControl);
        }

        private void SplitContainer_SplitterMoved(object sender, SplitterEventArgs e)
        {
            SplitContainer control = (SplitContainer)sender;
            MainForm.GlobalSplitWidth = this.splitContainer.Width - control.SplitterDistance;
        }

        private void ControlContainer_SizeChanged(object sender, EventArgs e)
        {
            Panel control = (Panel)sender;
            this.demo_control.Height = Math.Min(1000, (control.Height - 80));
            this.demo_control.Location = new Point((control.Width - this.demo_control.Width) / 2, (control.Height - this.demo_control.Height) / 2);
        }

        private void Demo_control_BackgroundDrawBefore(object sender, SingleAccordionBackgroundDrawBeforeEventArgs e)
        {
            Graphics g = e.Graphics;

            //渐变背景
            LinearGradientBrush back_sb = new LinearGradientBrush(e.Bounds_C, Color.FromArgb(139, 198, 236), Color.FromArgb(149, 153, 226), LinearGradientMode.Vertical);
            g.FillRectangle(back_sb, e.Bounds_C);
            back_sb.Dispose();

            //取消控件默认绘制
            e.Cancel = true;
        }

        private void Demo_control_NodeDrawBefore(object sender, SingleAccordionNodeDrawBeforeEventArgs e)
        {
            SingleAccordionCategoryNode category_node = (SingleAccordionCategoryNode)e.Node.Parent;
            if (e.Node is SingleAccordionContentNode && e.Node.Level > 0 && (category_node.ECAnimationStatus == SingleAccordionNodeECAnimationStatuss.Expanding || category_node.ECAnimationStatus == SingleAccordionNodeECAnimationStatuss.Collapseing))
            {
                SingleAccordion sa = (SingleAccordion)sender;
                Graphics g = e.Graphics;

                if (category_node.ECAnimationStatus == SingleAccordionNodeECAnimationStatuss.Expanding)
                {
                    float start_x = e.TextBounds_C.Right;
                    int index = -1;
                    for (int i = 0; i < category_node.Childrens.Count; i++)
                    {
                        if (category_node.Childrens[i] == e.Node)
                        {
                            index = i;
                            break;
                        }
                    }
                    float allTime = sa.CategoryNodeStyle.ECAnimationAllTimer * (index + 1) / (float)(category_node.Childrens.Count + 1);
                    float usedTime = Math.Min(category_node.ECAnimationUsedTime, allTime);
                    start_x = -start_x + start_x * usedTime / allTime;
                    // 图片绘制
                    g.DrawImage(e.Image, new RectangleF(start_x + e.ImageBounds_C.X, e.ImageBounds_C.Y, e.ImageBounds_C.Width, e.ImageBounds_C.Height));
                    // 文本绘制
                    g.DrawString(e.Node.Text, sa.Font, e.TextBrush, new RectangleF(start_x + e.TextBounds_C.X, e.TextBounds_C.Y, e.TextBounds_C.Width, e.TextBounds_C.Height), StringFormat.GenericTypographic);

                    //取消控件默认绘制
                    e.Cancel = true;
                }
                else if (category_node.ECAnimationStatus == SingleAccordionNodeECAnimationStatuss.Collapseing)
                {
                    float start_x = e.TextBounds_C.Right;
                    int index = -1;
                    for (int i = 0; i < category_node.Childrens.Count; i++)
                    {
                        if (category_node.Childrens[i] == e.Node)
                        {
                            index = category_node.Childrens.Count - 1 - i;
                            break;
                        }
                    }
                    float simgleTime = sa.CategoryNodeStyle.ECAnimationAllTimer / (float)category_node.Childrens.Count;
                    float usedTime = Math.Max(0, category_node.ECAnimationUsedTime - ((index) * simgleTime));
                    start_x = start_x * (usedTime / simgleTime);
                    // 图片绘制
                    g.DrawImage(e.Image, new RectangleF(e.ImageBounds_C.X - start_x, e.ImageBounds_C.Y, e.ImageBounds_C.Width, e.ImageBounds_C.Height));
                    // 文本绘制
                    g.DrawString(e.Node.Text, sa.Font, e.TextBrush, new RectangleF(e.TextBounds_C.X - start_x, e.TextBounds_C.Y, e.TextBounds_C.Width, e.TextBounds_C.Height), StringFormat.GenericTypographic);

                    //取消控件默认绘制
                    e.Cancel = true;
                }

            }
        }

        private void Demo_control_NodeDrawAfter(object sender, SingleAccordionNodeDrawAfterEventArgs e)
        {
            SingleAccordion sa = (SingleAccordion)sender;
            Graphics g = e.Graphics;

            //鼠标进入离开内容节点时出现有中心向两边渐变动画效果
            RectangleF rect = e.Bounds_C;
            if (sa.SelectedNode == e.Node)
            {
                e.BackBrush.Color = Color.FromArgb(50, 0, 0, 0);
                g.FillRectangle(e.BackBrush, rect);
            }
            else if (e.Node.MouseAnimationStatus == SingleAccordionNodeMouseAnimationStatuss.MouseEnter)
            {
                e.BackBrush.Color = Color.FromArgb(50, 0, 0, 0);
                g.FillRectangle(e.BackBrush, rect);
            }
            else if (e.Node.MouseAnimationStatus == SingleAccordionNodeMouseAnimationStatuss.MouseEntering)
            {
                double progress = AnimationHelper.EaseOut(e.Node.MouseAnimationUsedTime, e.Node.GetMouseAnimationAllTime(), 2.0);
                float height = Math.Min(rect.Height, (float)(rect.Height * progress));
                rect = new RectangleF(rect.X, rect.Y + (rect.Height - height) / 2, rect.Width, height);

                e.BackBrush.Color = Color.FromArgb(50, 0, 0, 0);
                g.FillRectangle(e.BackBrush, rect);
            }
            else if (e.Node.MouseAnimationStatus == SingleAccordionNodeMouseAnimationStatuss.MouseLeaveing)
            {
                double progress = 1 - AnimationHelper.EaseOut(e.Node.MouseAnimationUsedTime, e.Node.GetMouseAnimationAllTime(), 2.0);
                float height = Math.Min(rect.Height, (float)(rect.Height * progress));
                rect = new RectangleF(rect.X, rect.Y + (rect.Height - height) / 2, rect.Width, height);

                e.BackBrush.Color = Color.FromArgb(Math.Max(0, (int)(50 * progress)), 0, 0, 0);
                g.FillRectangle(e.BackBrush, rect);
            }
            else if (e.Node.MouseAnimationStatus == SingleAccordionNodeMouseAnimationStatuss.MouseLeave)
            {

            }

            //内容节单击时绘制圆形扩散动画效果
            SingleAccordionCustomAnimation customAnimation = sa.FindCustomAnimation("nodeclick", e.Node);
            if (customAnimation != null)
            {
                Color color_tmp = e.BackBrush.Color;
                e.BackBrush.Color = Color.FromArgb(30, 0, 0, 0);
                Point point = sa.PointToClient(Control.MousePosition);
                int animationRadius = (int)Math.Ceiling(ControlHelper.AdjustRectangleMaxRadiusForPoint(e.Bounds_C, point) * customAnimation.AnimationUsedTime / (float)customAnimation.AnimationAllTime * 10 / 8);
                g.FillEllipse(e.BackBrush, new RectangleF(point.X - animationRadius, point.Y - animationRadius, animationRadius * 2, animationRadius * 2));
                e.BackBrush.Color = color_tmp;
            }

        }

        private void Demo_control_NodeClick(object sender, SingleAccordionNodeEventArgs e)
        {
            //内容节单击时添加圆形扩散动画效果
            if (e.Node is SingleAccordionContentNode)
            {
                SingleAccordion sa = (SingleAccordion)sender;
                sa.AddCustomAnimation("nodeclick", e.Node, 0, 300, false, null);
            }
        }

        #endregion

    }
}
